package datastruct.sort;

import java.util.Arrays;

/**
 * @program: DataStructuresAndAlgorithms
 * @description: 冒泡排序   冒泡排序的时间复杂度 O(n²)
 * @author: zhang
 * @create: 2020-03-12 15:34
 */
public class BubbleSortDemo {

    public static void sort(int arr[]) {
        int temp;
        boolean flag = false;
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - i - 1; j++) {
                if (arr[j] > arr[j + 1]) {
                    flag = true;
                    temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
            if (!flag) {
                break;
            } else {
                flag = true;
            }
        }
    }

    public static void main(String[] args) {
        int[] arr = {1, 8, 9, 2, 3, 4, 7, 20, 5, 6};

        int[] arr2 = new int[80000];
        for (int i = 0; i < 80000; i++) {
            arr2[i] = (int) (Math.random() * 80000000); //生成[0,80000000]
        }
//        System.out.println(Arrays.toString(arr));
        long start = System.currentTimeMillis();
        System.out.println(start);
        sort(arr2);
        System.out.println(arr2.length);
        System.out.println(System.currentTimeMillis() - start);
        System.out.println(Arrays.toString(arr2));

//        System.out.println(Arrays.toString(arr));


        //        for (int i = 0; i < arr.length; i++) {
//            int temp;
//            for (int j = i + 1; j < arr.length; j++) {
//                if (arr[j] < arr[i]) {
//                    temp = arr[j];
//                    arr[j] = arr[i];
//                    arr[i] = temp;
//                }
//            }
//        }

    }
}